// pages/apply/factory/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: [],
		repair_factory_id: '',
		id:'',
		baseUrl:app.globalData.baseUrl
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options)
		this.setData({
			id: options.id
		})
		// this.onGetData()
	},
	onGerLocation() {
		let that = this;
		wx.getLocation({
			type: 'wgs84',
			success(res) {
				console.log(res)
				const latitude = res.latitude
				const longitude = res.longitude
				const speed = res.speed
				const accuracy = res.accuracy
				that.onGetData(latitude, longitude)
			}
		})
	},
	onGetData(lat, lng) {
		let that = this;
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/searchRepairFactoryLst',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				lat: lat,
				lng: lng
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					that.setData({
						data: res.data.data
					})
				}

			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	checkboxChange(e) {
		this.setData({
			repair_factory_id: e.detail.value
		})

		
	},
	onSubmit() {
		let that = this;
		if(!that.data.repair_factory_id){
			wx.showToast({
				title:'请先选择',
				icon:'none'
			})
			return false
		}
		wx.showLoading({
			title:'提交中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/appointRepairFactory',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				order_id: that.data.id,
				repair_factory_id: that.data.repair_factory_id
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					wx.showModal({
						title: '提示',
						content: '提交成功',
						showCancel: false,
						confirmColor:'#DC5640',
						confirmText:'确定',
						success(res) {
							wx.switchTab({
								url:'/pages/index/index'
							})
						}
					})
				}else {
					wx.showModal({
						title: '提示',
						content: '提交失败',
						showCancel: false,
						confirmColor:'#DC5640',
						confirmText:'确定',
						success(res) {
							
						}
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		let that = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.getSetting({
			success(res) {
				if (res.authSetting['scope.userLocation'] == false) { //如果没有授权地理位置
					wx.openSetting({
						success(res) {
							res.authSetting = { //打开授权位置页面，让用户自己开启
								"scope.userLocation": true
							}
						}
					})
				} else {
					//用户开启授权后可直接获取地理位置
					wx.authorize({
						scope: 'scope.userLocation',
						success() {
							//获取位置后相关操作
							that.onGerLocation();
						}
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
